package com.atguigu.gmall.list.receiver;


import com.atguigu.gmall.common.config.constant.MqCons;
import com.atguigu.gmall.list.service.SearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ListReceiver {

    @Autowired
    private SearchService searchService;

    //上架商品
   @RabbitListener(bindings = @QueueBinding(
           value = @Queue(value = MqCons.QUEUE_GOODS_UPPER),
           exchange = @Exchange(value = MqCons.EXCHANGE_DIRECT_GOODS),
           key = MqCons.ROUTING_GOODS_UPPER
   ))
    public void upperGoods(Long skuId,Message message, Channel channel) throws IOException {
       searchService.upperGoods(skuId);
       channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    //下架商品
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqCons.QUEUE_GOODS_LOWER),
            exchange = @Exchange(value = MqCons.EXCHANGE_DIRECT_GOODS),
            key = MqCons.ROUTING_GOODS_LOWER
    ))
    public void lowerGoods(Long skuId,Message message, Channel channel) throws IOException {
        searchService.lowerGoods(skuId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
